package com.example.upload.utils;

import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;

import java.io.File;
import java.util.Arrays;
import java.util.Objects;

/**
 * VideoToAudio
 *
 * @author ChenJiaGeng
 * @date 2024/10/17 17:26
 * @since 1.0.0
 */
public class VideoToAudio {

    //要输出的音频格式
    private static String outputFormat = "mp3";


    /**
     *获得转化后的文件名
     *@paramsourceFilePath:源视频文件路径
     *@return
     */
    public static String getNewFileName(String  sourceFilePath){
        File source= new File(sourceFilePath);
        String fileName=source.getName().substring(0,source.getName().lastIndexOf("."));
        return fileName+"."+outputFormat;
    }

    /**
     *转化音频格式
     *@paramsourceFilePath:源视频文件路径
     *@paramtargetFilePath:目标音乐文件路径
     *@return
     */
    public static void transform(String sourceFilePath,String targetFilePath){
        File source=new File(sourceFilePath);
        File target=new File(targetFilePath);
        //设置音频属性
        AudioAttributes audio=new AudioAttributes();
        audio.setCodec(null);
        //设置转码属性
        EncodingAttributes attrs=new EncodingAttributes();
        attrs.setOutputFormat(outputFormat);
        attrs.setAudioAttributes(audio);
        try{
            //音频转换格式类
            Encoder encoder=new Encoder();
            MultimediaObject mediaObject=new MultimediaObject(source);
            encoder.encode(mediaObject,target,attrs);
            System.out.println("转换已完成...");
        }catch(EncoderException e){
            e.printStackTrace();
        }
    }

    /**
     *批量转化音频格式
     *@paramsourceFolderPath:源视频文件夹路径
     *@paramtargetFolderPath:目标音乐文件夹路径
     *@return
     */
    public static void batchTransform(String sourceFolderPath, String targetFolderPath){
        File sourceFolder=new File(sourceFolderPath);
        if(Objects.requireNonNull(sourceFolder.list()).length!=0){
            Arrays.asList(Objects.requireNonNull(sourceFolder.list())).forEach(e->{
                transform(sourceFolderPath+"\\"+e,targetFolderPath+"\\"+getNewFileName(e));
            });
        }
    }

    /**
     *单个文件转化音频格式
     *@paramsourceFolderPath:源视频文件夹路径
     *@paramtargetFolderPath:目标音乐文件夹路径
     *@return
     */
    public static String singleTransform(String sourceFilePath, String targetFilePath){
        File sourceFile = new File(sourceFilePath);

        // 检查源文件是否存在
        if (sourceFile.exists() && sourceFile.isFile()) {
            // 调用转换方法，将源文件转换为目标文件
            transform(sourceFilePath, targetFilePath +"/"+getNewFileName(sourceFilePath));
            return targetFilePath +"/"+getNewFileName(sourceFilePath);
        } else {
            return null;
        }
    }



    public static void main(String[]args){
        batchTransform("D:/000-total_folder/301-Files/video","D:/000-total_folder/301-Files/audio");
    }

}
